<template>
  <div>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="9">
          <el-input clearable v-model="queryInfo.query" placeholder="请输入仓库名" prefix-icon="el-icon-search">
            <el-button slot="append" icon="el-icon-search" @click="getStorehouseList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addStorehouseDialog">新增</el-button>
        </el-col>
      </el-row>
      <el-table :data="storehouseList.slice((queryInfo.pageNum-1)*queryInfo.pageSize,queryInfo.pageNum*queryInfo.pageSize)"
      style="width: 100%"
      stripe border fixed height="500">
          <el-table-column
            fixed="left"
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="name"
            label="仓库名称">
          </el-table-column>
          <el-table-column
            prop="simpleName"
            label="仓库简称">
          </el-table-column>
          <el-table-column
            prop="addr"
            label="仓库地址">
          </el-table-column>
          <el-table-column
            prop="remarks"
            label="备    注">
          </el-table-column>
          <el-table-column
            fixed="right"
            prop="operate"
            label="操作"
            width="180">
            <template slot-scope="scope" >
              <el-button type="primary" icon="el-icon-edit" size="small" @click="updataStorehousedialog(scope.row)">修改</el-button>
              <el-button type="danger" icon="el-icon-delete" size="small" @click="deleteStorehouse(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

       <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryInfo.pageNum"
              :page-sizes="[10, 20, 30, 40]"
              :page-size="queryInfo.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
        </el-pagination>
      </el-card>

      <el-dialog :title="title" :visible.sync="storehouseDialogVisible" width="65%" @close="closeStorehouseDialog">

        <el-form :model="storehouseForm" :rules="rules" ref="storehouseRuleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="仓库名称" prop="name">
            <el-input v-model="storehouseForm.name" ></el-input>
          </el-form-item>
          <el-form-item label="仓库别名" prop="simpleName">
            <el-input v-model="storehouseForm.simpleName" ></el-input>
          </el-form-item>
          <el-form-item label="仓库地址" prop="addr">
            <el-input v-model="storehouseForm.addr" ></el-input>
          </el-form-item>
          <el-form-item label="备    注" prop="remarks">
            <el-input v-model="storehouseForm.remarks" ></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="storehouseDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="operationStorehouse">确 定</el-button>
        </div>
      </el-dialog>

  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      storehouseList: [],
      storehouseDialogVisible: false,
      title: '新增仓库',
      storehouseForm: {
        name: '',
        simpleName: '',
        addr: '',
        remarks: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入仓库名称', trigger: 'blur' }
        ],
        simpleName: [
          { required: true, message: '请输入仓库简称', trigger: 'blur' }
        ],
        addr: [
          { required: true, message: '请输入仓库地址', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSizeChange (val) {
      this.queryInfo.pageSize = val
      this.getStorehouseList()
    },
    handleCurrentChange (val) {
      this.queryInfo.pageNum = val
      this.getStorehouseList()
    },
    async getStorehouseList () {
      const { data: result } = await this.$http.get('/storehouse/list', { params: this.queryInfo })
      if (result.status !== 200) return this.$message.error('获取列表失败')

      this.total = result.data.total
      this.storehouseList = result.data.rows
    },
    closeStorehouseDialog () {
      this.$refs.storehouseRuleForm.resetFields()
    },
    addStorehouseDialog () {
      this.title = '新增仓库'
      this.storehouseDialogVisible = true
    },
    updataStorehousedialog (storehouse) {
      this.storehouseForm.name = storehouse.name
      this.storehouseForm.simpleName = storehouse.simpleName
      this.storehouseForm.addr = storehouse.addr
      this.storehouseForm.remarks = storehouse.remarks
      this.title = '修改仓库'
      this.storehouseDialogVisible = true
    },
    addStorehouse () {
      this.$refs.storehouseRuleForm.validate(async valid => {
        const { data: result } = await this.$http.post('/storehouse/addStorehouse', this.storehouseForm)
        if (result.status !== 200) return this.$message.error('添加仓库失败')
        this.$message.success('成功添加仓库')
        this.getStorehouseList()
        this.storehouseDialogVisible = false
      })
    },
    async updataStorehouse () {
      const { data: result } = await this.$http.put('/storehouse/updataStorehouse', this.storehouseForm)
      if (result.status !== 200) return this.$message.error('修改仓库失败')
      this.$message.success('更新成功')
      this.getStorehouseList()
      this.storehouseDialogVisible = false
    },
    operationStorehouse () {
      if (this.title === '新增仓库') return this.addStorehouse()
      if (this.title === '修改仓库') {
        this.updataStorehouse()
      }
    },
    async deleteStorehouse (storehouse) {
      const result = await this.$confirm('此操作将永久删除' + storehouse.name + ', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      if (result !== 'confirm') return this.$message.info('用户取消操作')
      const { data: resultDB } = await this.$http.delete(`/storehouse/delete/${storehouse.name}`)
      if (resultDB.status !== 200) return this.$message.error('删除出库失败')
      this.getStorehouseList()
      this.$message.success('仓库删除成功')
    }
  },
  mounted () {
    this.getStorehouseList()
  }
}
</script>

<style>
</style>
